package cn.com.tom.dao.impl;

import cn.com.tom.bean.Award;
import cn.com.tom.dao.AwardDao;
import cn.com.tom.util.Dbs;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;

public class AwardDaoImpl extends BaseDaoImpl<Award> implements AwardDao {

    @Override
    public boolean save(Award bean) {
        Integer id = bean.id;
        String sql = "";
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getImage());
        args.add(bean.getIntro());

        if (id == null || id < 1) {
            sql = "INSERT INTO award(name,image,intro) VALUES(?,?,?)";
        } else {
            sql = "UPDATE award SET name=?,image=?,intro=? WHERE id=?";
            args.add(id);
        }
        return Dbs.save(sql, args.toArray());
    }

    @Override
    public Award get(Integer id) {
        String sql = "SELECT id,created_time,name,image,intro FROM award WHERE id=?";
        return Dbs.queryForObject(sql, new BeanPropertyRowMapper<>(Award.class), id);
    }

    @Override
    public List<Award> list() {
        String sql = "SELECT id,created_time,name,image,intro FROM award";
        return Dbs.list(sql, new BeanPropertyRowMapper<>(Award.class));
    }

}
